home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / misc / amigem.lha / amigem / amilo / amilo.c next >
Encoding:
C/C++ Source or Header  |  1995-02-02  |  4.6 KB  |  230 lines

  1. #include <exec/resident.h>
  2. #include <exec/memory.h>
  3. #include <dos/doshunks.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <signal.h>
  7. #include <setjmp.h>
  8. #include <fcntl.h>
  9. #include <unistd.h>
  10. #include <sys/time.h>
  11. #include <exec/io.h>
  12. #include <exec/semaphores.h>
  13. #include <clib/exec_protos.h>
  14. #include <clib/utility_protos.h>
  15. #include <amigem/machine.h>
  16.  
  17. #include <amigem/fd_lib.h>
  18.  
  19. void *const basetabl[]=
  20. {
  21.   NULL,NULL,NULL,NULL,NULL, /* 0 */
  22.   NULL,NULL,NULL,NULL,NULL,
  23.   &CacheClearU,    /* 10 */
  24.   &CacheClearE,
  25.   &CachePreDMA,
  26.   &CachePostDMA,
  27.   NULL,NULL,NULL,NULL,NULL,NULL,
  28.   &SDivMod32,    /* 20 */
  29.   &SMult32,
  30.   &SMult64,
  31.   &UDivMod32,
  32.   &UMult32,
  33.   &UMult64,
  34.   NULL,NULL,NULL,NULL,
  35.   &malloc,    /* 30 */
  36.   NULL,NULL,NULL,NULL,
  37.   (void *)&exit,    /* 35 */
  38.   &setjmp,
  39.   &longjmp,
  40.   NULL,NULL,
  41.   &kill,    /* 40 */
  42.   &getpid,
  43.   NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
  44.   &sigprocmask,    /* 50 */
  45.   &sigsuspend,
  46.   &sigaction,
  47.   NULL,NULL,NULL,NULL,NULL,NULL,NULL,
  48.   &open,    /* 60 */
  49.   &close,
  50.   &read,
  51.   &write,&puts,
  52.   NULL,NULL,NULL,NULL,NULL,
  53.   NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
  54.   &setitimer,    /* 80 */
  55. };
  56.  
  57. void readall(int file,void *buf,int size)
  58. {
  59.   char *b=buf;
  60.   do
  61.   {
  62.     int l;
  63.     if((l=read(file,b,size))<0)
  64.     {
  65.       fputs("error reading file\n",stderr);
  66.       exit(20);
  67.     }
  68.     b+=l;
  69.     size-=l;
  70.   }while(size);
  71. }
  72.  
  73. char *allocmem(size_t size)
  74. {
  75.   char *ret;
  76.   if((ret=malloc(size))==NULL)
  77.   {
  78.     fputs("out of memory\n",stderr);
  79.     exit(20);
  80.   }
  81.   return ret;
  82. }
  83.  
  84. void *loadseg(char *filename)
  85. {
  86.   unsigned long    a,b,first,count=0,i;
  87.   int read=0;
  88.  
  89.   int file;
  90.   unsigned long numsegs=0;
  91.   char **hunks;
  92.   unsigned long *hunksizes;
  93.  
  94.   if((file=open(filename,O_RDONLY,0))<0)
  95.     goto error;
  96.   readall(file,&a,sizeof(unsigned long));
  97.   if(a!=HUNK_HEADER)
  98.     goto error;
  99.   for(;;)
  100.   {
  101.     readall(file,&a,sizeof(unsigned long));
  102.     if(!a)
  103.       break;
  104.     if(lseek(file,a*sizeof(unsigned long),SEEK_CUR)<0)
  105.       goto error;
  106.   }
  107.   readall(file,&numsegs,sizeof(unsigned long));
  108.   if(!numsegs)
  109.     goto error;
  110.   readall(file,&first,sizeof(unsigned long));
  111.   readall(file,&a,sizeof(unsigned long));
  112.   if(a-first!=numsegs-1)
  113.     goto error;
  114.   hunks=alloca(numsegs*sizeof(char *));
  115.   hunksizes=alloca(numsegs*sizeof(unsigned long));
  116.   for(i=0;i<numsegs;i++)
  117.   {
  118.     readall(file,&a,sizeof(unsigned long));
  119.     if(a&(HUNKF_CHIP|HUNKF_FAST))
  120.       goto error;
  121.     a*=sizeof(unsigned long);
  122.     hunks[i]=allocmem(a);
  123.     hunksizes[i]=a;
  124.   }
  125.  
  126.   do
  127.   {
  128.     readall(file,&a,sizeof(unsigned long));
  129.     switch(a)
  130.     {
  131.       case HUNK_CODE:
  132.       case HUNK_DATA:
  133.       case HUNK_BSS:
  134.         readall(file,&b,sizeof(unsigned long));
  135.         b*=sizeof(unsigned long);
  136.         if(b>hunksizes[count])
  137.           goto error;
  138.         if((a&~(HUNKF_CHIP|HUNKF_FAST))!=HUNK_BSS)
  139.           readall(file,hunks[count],b);
  140.         read=1;
  141.         break;
  142.       case HUNK_ABSRELOC32:
  143.         if(!read)
  144.           goto error;
  145.         for(;;)
  146.         {
  147.           readall(file,&a,sizeof(unsigned long));
  148.           if(!a)
  149.             break;
  150.           readall(file,&i,sizeof(unsigned long));
  151.           i-=first;
  152.           if(i>=numsegs)
  153.             goto error;
  154.           while(a--)
  155.           {
  156.             readall(file,&b,sizeof(unsigned long));
  157.             if(b>=hunksizes[count]-sizeof(unsigned long))
  158.               goto error;
  159.             *(unsigned long *)&hunks[count][b]+=(unsigned long)hunks[i];
  160.           }
  161.         }        
  162.         break;
  163.       case HUNK_SYMBOL:
  164.         for(;;)
  165.         {
  166.           readall(file,&a,sizeof(unsigned long));
  167.           if(!a)
  168.             break;
  169.           if(lseek(file,((a&0xffffff)+1)*sizeof(unsigned long),SEEK_CUR)<0)
  170.             goto error;
  171.         }
  172.         break;
  173.       case HUNK_DEBUG:
  174.         readall(file,&a,sizeof(unsigned long));
  175.         if(lseek(file,a*sizeof(unsigned long),SEEK_CUR)<0)
  176.           goto error;
  177.         break;
  178.       case HUNK_END:
  179.         count++;
  180.         read=0;
  181.         break;
  182.       default:
  183.         goto error;
  184.     }
  185.   }while(count<numsegs);
  186.   
  187.   if(close(file))
  188.   { file=0;
  189.     goto error; }
  190.  
  191.   CacheClearU();
  192.  
  193.   return hunks[0];
  194.  
  195. error:
  196.   
  197.   fputs("Missing or malformatted load file: ",stderr);
  198.   fputs(filename,stderr);
  199.   fputs("\n",stderr);
  200.   exit(20);
  201.   return 0;
  202. }
  203.  
  204. FC3(0,LONG,Init,A1,APTR dummy1,D0,BPTR dummy2,A0,APTR ft,A6)
  205. ;
  206.  
  207. void startromtag(void *seg)
  208. {
  209.   UWORD *w=(UWORD *)seg;
  210.  
  211.   for(;;)
  212.   {
  213.     struct Resident *r=(struct Resident *)w;
  214.     if(r->rt_MatchWord==RTC_MATCHWORD&&r==r->rt_MatchTag)
  215.       Init(r->rt_Init,NULL,0,(APTR)basetabl);
  216.     w++;
  217.   }
  218. }
  219.  
  220. int main(int argc,char *argv[])
  221. {
  222.   void *rom;
  223.  
  224.   rom=loadseg("Devs/amigem");  /* load amigem image */
  225.  
  226.   startromtag(rom);
  227.  
  228.   return 0;
  229. }
  230.